﻿using System;
using System.Text;
using System.Web;

namespace SharpBase.Web
{
    /// <summary>
    /// Encapsulates the Uri and form parameters of a web form
    /// </summary>
    public class WebForm
    {
        private Uri _Uri;
        private WebFormNameValuePairCollection _FormParams;

        /// <summary>
        /// Provides direct access to the Form Parameters collection
        /// </summary>
        public WebFormNameValuePairCollection FormParametersCollection
        {
            get { return _FormParams; }
        }

        /// <summary>
        /// Initializes the WebForm with the specified Uri
        /// </summary>
        /// <param name="uri">Uri</param>
        public WebForm(string uri)
            : this(new Uri(uri))
        { }

        /// <summary>
        /// Initializes the WebForm with the specified Uri
        /// </summary>
        /// <param name="uri">Uri</param>
        public WebForm(Uri uri)
        {
            _Uri = uri;
            _FormParams = new WebFormNameValuePairCollection();
        }

        /// <summary>
        /// Gets/sets the Uri associated with the Web Form
        /// </summary>
        public Uri Uri
        {
            get { return _Uri; }
            set { _Uri = value; }
        }

        /// <summary>
        /// Returns the length of the form content -- use sparingly as it generates the query string to calculate the length
        /// </summary>
        public int ContentLength
        {
            get
            {
                int length = 0;
                foreach (WebFormNameValuePair nvp in _FormParams)
                {
                    if (length > 0)
                        length += 1;
                    length += Web.URLEncode(nvp.Name).Length;
                    length += Web.URLEncode(nvp.Value).Length;
                    length++;
                }
                return length;
            }
        }

        /// <summary>
        /// Adds a form parameter to the web form
        /// </summary>
        /// <param name="name">Name of the parameter</param>
        /// <param name="value">Value of the parameter</param>
        public void AddFormParameter(string name, string value)
        {
            _FormParams.Add(new WebFormNameValuePair(name, value));
        }

        /// <summary>
        /// Returns the formatted parameter string for the web form
        /// </summary>
        /// <returns>Returns the formatted parameter string for the web form</returns>
        public string GetWebFormString()
        {
            StringBuilder sb = new StringBuilder();
            foreach(WebFormNameValuePair nvp in _FormParams)
            {
                if (sb.Length > 0)
                    sb.Append("&");
                sb.Append(Web.URLEncode(nvp.Name));
                sb.Append("=");
                sb.Append(Web.URLEncode(nvp.Value));
            }
            return sb.ToString();
        }
    }
}
